package com.basic.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.system.domain.SysNotice;
import com.basic.system.service.ISysNoticeService;

/**
 * 公告 信息操作处理
 * 
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Resource
    private ISysNoticeService noticeService;

    /**
     * 获取公告管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.getList(notice);
        return getDataTable(list);
    }

    /**
     * 根据公告管理编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(noticeService.get(id));
    }

    /**
     * 新增公告管理
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        return toAjax(noticeService.insert(notice));
    }

    /**
     * 修改公告管理
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        return toAjax(noticeService.update(notice));
    }

    /**
     * 删除公告管理
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(noticeService.deleteByIds(ids));
    }

    /**
     * 状态变更
     */
    @PreAuthorize("@ss.hasPermi('business:notice:changeStatus')")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestParam("ids") String[] ids, @RequestParam("status") String status)
    {
        return toAjax(noticeService.updateStatusByIds(ids, status));
    }

    /**
     * 查询是否存在已发布的公告，排除自身公告
     */
    @GetMapping("/isExistPublishedNotice/{id}")
    public AjaxResult isExistPublishedNotice(@PathVariable String id)
    {
        return AjaxResult.success(noticeService.isExistPublishedNotice(id));
    }

    /**
     * 查询已发布的公告
     */
    @GetMapping("/getPublishedNotice")
    public AjaxResult getPublishedNotice()
    {
        return AjaxResult.success(noticeService.getPublishedNotice());
    }

}
